<template>
  <div class="search-new">
    <!-- <h3 class="search-title">查新检索</h3> -->
    <div>
      <div v-for="(item, i) in rowData" :key="i" class="a-row">
        <div class="label">
          {{item.label + (i > 1 ? i : '')}}
        </div>
        <div class="row-input">
          <input v-if="i === 0" v-model="item.value" :placeholder="item.placeholder" class="input-item" />
          <el-input v-else type="textarea" :autosize="{ minRows: 1, maxRows: 3}" v-model="item.value" :placeholder="item.placeholder" class="input-item"></el-input>
          <el-color-picker v-model="item.color" class="color-picker" :predefine="predefineColors"></el-color-picker>
        </div>
        <svg v-if="i === 0" class="icon-add" style="opacity: 0;cursor: default;" aria-hidden="true" width="24" height="24">
          <use xlink:href="#icon-tianjia"></use>
        </svg>
        <svg v-if="i === 1" class="icon-add" aria-hidden="true" width="24" height="24" @click="rowBtnClick(1, i)">
          <use xlink:href="#icon-tianjia"></use>
        </svg>
        <svg v-if="i > 1" class="icon-add" aria-hidden="true" width="24" height="24" @click="rowBtnClick(-1, i)">
          <use xlink:href="#icon-jianqu"></use>
        </svg>
      </div>

      <div class="search-footer">
        <div class="data-source btn" @click="openDataSource">
          <i class="iconfont icon-xuanzeshujuyuan_new"></i>
          <span>选择数据源</span>
        </div>
        <el-button @click="search" class="searchBtn btn">检索</el-button>
      </div>
    </div>
    <!-- 数据来源 -->
    <!-- <DataSource ref="dataSource"/> -->
  </div>
</template>

<script type="text/ecmascript-6">
// import DataSource from '../DataSource'
import Ecolors from "@/utils/chartsColors.js";
export default {
  name: "fto",
  // components: {
  //   DataSource
  // },
  data() {
    return {
      predefineColors:Ecolors.color.slice(0,10),
      dataSourceShow: false,
      rowData: [
        {
          label: '技术领域',
          sq: ['B/'],
          color: Ecolors.color[0],
          value: '',
          placeholder:"请输入关键词"
        },
        {
          label: '技术特征',
          sq: ['R/'],
          color: Ecolors.color[0],
          value: '',
          placeholder:"选填，输入关键词或一段话"
        }
      ]
    }
  },
  methods: {
    /*
    * params: 1:加， -1减, 索引
    * return: 无
    * function: 行增加或减
    * */
    rowBtnClick(type, i) {
      if (type === 1) {
        this.rowData.push({
          label: '技术特征',
          sq: ['R/'],
          color: Ecolors.color[0],
          value: '',
          placeholder:"选填，输入关键词或一段话"
        });
      } else if (type === -1) {
        this.rowData.splice(i, 1);
      }
    },
    /*
    * params: 无
    * return: 检索式
    * function: 获取检索式
    * */
    getSq() {
      let arr = [];
      let highlightKeyWords = [];
      this.rowData.forEach(item => {
        let a = [];
        if (item.value) {
          item.sq.forEach(x => {
            a.push(x + item.value);
          });
          arr.push('(' + a.join(' AND ') + ')');
          highlightKeyWords.push({
            words: item.value.trim(),
            color: item.color
          });
        }
      });

      this.$store.commit("setHighlightKeyWords", {
        modes: highlightKeyWords,
        open: true
      })

      return arr.join(' AND ');
    },
    /*
    * params: 无
    * return: 无
    * function: 检索
    * */
    search() {
      // let key = `sq${new Date().getTime()}`;
      let sq = this.getSq();
      if (!sq) {
        this.$message({
          type: 'error',
          duration: 1500,
          message: '请输入内容'
        });
        return false;
      }
      
      this.$store.commit("setSearchWord", sq);

      // 暂时先注释
      // this.$nextTick(() => {
      //   this.$router.push({
      //     name: 'PLSearchResult'
      //   })
      // });
      // sessionStorage.setItem(key, sq);//用于检索
      // window.sessionStorage.setItem('userSearch', 'true');
      // window.open('loggedIn.html#/SearchResult?key=' + key, '_blank');
    },
    // 选择数据源
    openDataSource() {
      this.$emit("openDataSource");
      // this.$refs.dataSource.init()
    },
  }
}
</script>

<style lang="scss" scoped>
.search-new {
  position: relative;
  width: 100%;
  padding: 20px 10px 80px 20px !important;
  border: 2px solid $color141 !important;
  border-radius: 8px;
  margin-top: 6px !important;
  box-sizing: border-box;
  background-color: #fff;
  * {
    box-sizing: border-box;
  }
  .search-title {
    height: 38px;
    line-height: 38px;
    font-size: 18px;
    color: $mainColor;
    font-weight: 700;
    margin-bottom: 20px;
  }

  .a-row {
    display: flex;
    align-items: center;
    min-height: 40px;
    margin-bottom: 20px;
    .label {
      flex: none;
      height: 100%;
      line-height: 40px;
      width: 180px;
      padding-left: 20px;
      margin-right: 10px;
      border-radius: 3px;
      background: #eef3fa;
    }
    .el-input__inner {
      font-size: 12px;
      padding-left: 0;
      background-color: #eef3fa;
      border: 0;
    }
    .el-textarea__inner {
      background: none;
      border: none;
      margin: 5px 0 0 0;
      padding-left:0;
      &::placeholder {
        color: #757575;
      }
    }
    .row-input {
      flex: auto;
      position: relative;
      margin: 0;
      padding-right: 20px;

      .input-item {
        width: 100%;
        min-height: 40px;
        line-height: 40px;
        padding: 0 40px 0 20px;
        border: none;
        border-radius: 3px;
        outline: none;
        background: #eef3fa;
      }
      .color-picker {
        position: absolute;
        right: 40px;
        top: 1px;
        width: 15px;
        height: 15px;

        .el-color-picker__trigger {
          width: 100%;
          height: 100%;
          padding: 0;
          border: none;
        }
        .el-color-picker__color {
          border: none;
        }
        .el-color-picker__icon {
          display: none;
        }
      }
    }
    .icon-add {
      cursor: pointer;
    }
  }

  .search-footer {
    position: relative;
    height: 100px;
    .data-source {
      display: inline-flex;
      align-items: center;
      justify-content: flex-start;
      height: 30px;
      color: $mainColor;
      i {
        font-size: 20px;
        cursor: pointer;
      }
      span {
        font-size: 14px;
        cursor: pointer;
      }
    }

    .searchBtn {
      position: absolute;
      right: 40px;
      bottom: 10px;
      font-size: 12px;
      padding: 8px 37px;
      background-color: $mainColor;
      color: #fff;
      font-weight: bold;
      border-radius: 15px;
      border: 1px solid $mainColor;
    }
  }
}
</style>
